using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
namespace Orbifold.Trefoil
{
    /// <summary>
    /// Assuming that a shape is inheriting from Control this tool looks for a TextBlock with the name
    /// 'BodyText' in the template and sets the TextDecoration collection to Underline. 
    /// </summary>
    class MakeUnderlineTool : ToolBase
    {
        private void PrintCommand()
        {

            PrintDialog pd = new PrintDialog();

            if ((pd.ShowDialog() == true))
            {

                //use either one of the below     

                pd.PrintVisual(SelectionManager.TextEditor as Visual, "printing as visual");

                pd.PrintDocument((((IDocumentPaginatorSource)SelectionManager.TextEditor.Document).DocumentPaginator), "printing as paginator");

            }

        }
        public override void OnExecute(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            LinearGradientBrush brush = App.Current.Resources["NormalBackgroundBrush"] as LinearGradientBrush;
            brush = new LinearGradientBrush(Colors.White, Colors.Red, 45);
            App.Current.Resources["NormalBackgroundBrush"] = brush;
            return;
            if (SelectionManager.SelectedText != null)
            {

                //make it undoable
                MakeUnderlineCommand cmd = new MakeUnderlineCommand(SelectionManager.SelectedText);
                UndoManager.Execute(cmd);

            }
            
        }
        /// <summary>
        /// Determines if a command is enabled. Override to provide custom behavior. Do not call the
        /// base version when overriding.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryEnabled(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {

            e.CanExecute = (SelectionManager.SelectedText.Text.Length > 0);
            e.Handled = true;
        }

        public MakeUnderlineTool()
            : base("Make underline")
        {

        }

        #region UnderlineCommand
        class MakeUnderlineCommand : ICommand
        {
            #region Fields
            private TextRange mText;
            private TextPointer ps, pe;
            #endregion

            #region Constructor
            ///<summary>
            ///Default constructor
            ///</summary>
            public MakeUnderlineCommand(TextRange text)
            {
                mText = text;
                ps = mText.Start;
                pe = mText.End;
            }
            #endregion


            #region ICommand Members

            public void Undo()
            {
                FlipUnderline();
            }

            public void Redo()
            {
                FlipUnderline();
            }
            private void FlipUnderline()
            {
                SelectionManager.TextEditor.Selection.Select(ps, pe);
                if (mText.GetPropertyValue(Inline.TextDecorationsProperty) == DependencyProperty.UnsetValue) //happens in a mixed situation
                {
                    mText.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
                    return;
                }
                else
                {
                    TextDecorationCollection o = (TextDecorationCollection)mText.GetPropertyValue(Inline.TextDecorationsProperty);
                    if (o == null) return;
                    if (o.Contains(System.Windows.TextDecorations.Underline[0]))
                        mText.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
                    else
                        mText.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
                }
            }
            public string Text
            {
                get
                {
                    return "Underline text";
                }
                set
                {
                    throw new Exception("The method or operation is not implemented.");
                }
            }

            #endregion
        }
        #endregion
    }
    
}
